package com.wangrui.test;

import com.wangrui.dao.UserMapper6;
import com.wangrui.entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class MyBatisTest06 {

    /**
     * 获得MyBatis SqlSessionFactory
     * SqlSessionFactory负责创建SqlSession，一旦创建成功，就可以用SqlSession实例来执行映射语句
     * ，commit，rollback，close等方法。
     *
     * @return
     */
    private static SqlSessionFactory getSessionFactory() {
        SqlSessionFactory sessionFactory = null;
        String resource = "configuration.xml";
        try {
            sessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader(resource));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sessionFactory;
    }


    @Test
    public void testList() {
        SqlSessionFactory factory = getSessionFactory();
        //默认是手动提交的
        SqlSession session = factory.openSession();
        UserMapper6 userMapper = session.getMapper(UserMapper6.class);
        User user = new User();
        user.setName("ang");
        user.setMinAge(20);
        user.setMaxAge(30);
        List<User> userList = userMapper.selectByCondition(user);
        System.out.println(userList.size());
    }

    @Test
    public void testList2() {
        SqlSessionFactory factory = getSessionFactory();
        //默认是手动提交的
        SqlSession session = factory.openSession();
        UserMapper6 userMapper = session.getMapper(UserMapper6.class);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", "ang");
        map.put("minAge", 20);
        map.put("maxAge", 30);
        List<User> userList = userMapper.selectByMap(map);
        System.out.println(userList.size());
    }

    @Test
    public void testList21() {
        SqlSessionFactory factory = getSessionFactory();
        //默认是手动提交的
        SqlSession session = factory.openSession();
        UserMapper6 userMapper = session.getMapper(UserMapper6.class);
        List<User> userList = userMapper.selectByMap2(20, 30);
        System.out.println(userList.size());
    }

    @Test
    public void testList3() {
        SqlSessionFactory factory = getSessionFactory();
        SqlSession session = factory.openSession();
        UserMapper6 userMapper = session.getMapper(UserMapper6.class);
        List<Map<String, Object>> list = userMapper.selectForMap();
        System.out.println(list);
    }

    @Test
    public void testList4() {
        SqlSessionFactory factory = getSessionFactory();
        SqlSession session = factory.openSession();
        UserMapper6 userMapper = session.getMapper(UserMapper6.class);
        List<User> userList = userMapper.selectByMultiParam("wangrui", 23);
        userList.forEach(u -> System.out.println(u.getName() + "," + u.getAge()));
    }

    @Test
    public void testList5() {
        SqlSessionFactory factory = getSessionFactory();
        SqlSession session = factory.openSession();
        UserMapper6 userMapper = session.getMapper(UserMapper6.class);
        List<User> userList = userMapper.selectByNamedParam("wangrui", 23);
        userList.forEach(u -> System.out.println(u.getName() + "," + u.getAge()));
    }

    @Test
    public void testList6() {
        SqlSessionFactory factory = getSessionFactory();
        SqlSession session = factory.openSession();
        UserMapper6 userMapper = session.getMapper(UserMapper6.class);
        User user = new User();
        user.setAge(23);
        List<User> userList = userMapper.selectByCompressParam("wangrui", user);
        userList.forEach(u -> System.out.println(u.getName() + "," + u.getAge()));
    }

    @Test
    public void testList7() {
        SqlSessionFactory factory = getSessionFactory();
        SqlSession session = factory.openSession();
        UserMapper6 userMapper = session.getMapper(UserMapper6.class);
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(1);
        ids.add(2);
        List<User> userList = userMapper.selectByListParam1(ids);
        userList.forEach(u -> System.out.println(u.getName() + "," + u.getAge()));
    }

    @Test
    public void testList8() {
        SqlSessionFactory factory = getSessionFactory();
        SqlSession session = factory.openSession();
        UserMapper6 userMapper = session.getMapper(UserMapper6.class);
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(1);
        ids.add(2);
        List<User> userList = userMapper.selectByListParam2(ids, "wangrui");
        userList.forEach(u -> System.out.println(u.getName() + "," + u.getAge()));
    }

    @Test
    public void testDate() {
        SqlSessionFactory factory = getSessionFactory();
        SqlSession session = factory.openSession();
        UserMapper6 userMapper = session.getMapper(UserMapper6.class);
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime birth = LocalDateTime.parse("2016-10-15 21:16:45", dtf);
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = birth.atZone(zoneId);
        Date birthDate = Date.from(zdt.toInstant());
        List<User> userList = userMapper.selectByDataCondition(birthDate);
        System.out.println(userList.size());
    }
}
